package cn.dnuihyy406.dnuifood_16110100406.controller.getfood;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.CollectStatus;
import cn.dnuihyy406.dnuifood_16110100406.bean.FoodBean;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.controller.LoginActivity;
import cn.dnuihyy406.dnuifood_16110100406.controller.RegisterActivity;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.BaseModel;
import cn.dnuihyy406.dnuifood_16110100406.model.GetfoodModel;

import static android.view.View.*;

public class GetFoodActivity extends AppCompatActivity implements OnRetrofitListener<Object>{
    private ImageView food_pic,link_pic,collect_pic;
    private TextView food_name,price,intro;
    private Button pinglun,purchase;
    private String food_id,foodname,price1,intro1,pic;
    private Context mContext;
    private RecyclerView recyclerView;
    private GetCommentsAdapter getCommentsAdapter;
    SharedPreferences sharedPreferences;
    private String user_id;
    String result="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_food);
        initViews();
        initData();
        initEvents();
    }
    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case 5:
                result = ((CollectStatus)object).getCollected();
                if(result.equals("0"))
                { collect_pic.setImageResource(android.R.drawable.btn_star_big_off); }
                else
                { collect_pic.setImageResource(android.R.drawable.btn_star_big_on); }
                break;
            case 6:
                if(((ResultBean)object).getSuccess().equals("1")){
                    if(result.equals("0")){
                        collect_pic.setImageResource(android.R.drawable.btn_star_big_on);
                        Toast.makeText(GetFoodActivity.this,"菜品收藏成功",Toast.LENGTH_SHORT).show();
                        result="1";
                    }
                    else {
                        collect_pic.setImageResource(android.R.drawable.btn_star_big_off);
                        Toast.makeText(GetFoodActivity.this,"取消菜品收藏成功",Toast.LENGTH_SHORT).show();
                        result="0";
                    }
                }
                else {
                    Toast.makeText(GetFoodActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case 7:
                if(!((FoodBean) object).getPic().equals("")){
                    Picasso.with(GetFoodActivity.this).load(BaseModel.BaseUrl+((FoodBean) object).getPic()).into(food_pic);
                }
                food_name.setText(((FoodBean) object).getFoodname());
                intro.setText(((FoodBean) object).getIntro());
                price.setText(((FoodBean) object).getPrice()+"元");
                break;
            case 8:
                getCommentsAdapter=new GetCommentsAdapter(GetFoodActivity.this,(List) object);
                recyclerView.setAdapter(getCommentsAdapter);
                break;
            default:
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(GetFoodActivity.this,"获取错误",Toast.LENGTH_SHORT).show();
    }

    void initViews(){
        food_pic=findViewById(R.id.food_pic);
        collect_pic=findViewById(R.id.collect_pic);
        food_name=findViewById(R.id.food_name);
        price=findViewById(R.id.price);
        intro=findViewById(R.id.intro);
        purchase=findViewById(R.id.purchase);
        recyclerView=this.findViewById(R.id.recyclerView2);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
    void initData(){
        sharedPreferences = getSharedPreferences("jzd", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
        food_id=getIntent().getStringExtra("food_id");

//        foodname=getIntent().getStringExtra("foodname");
        price1=getIntent().getStringExtra("price");
//        intro1=getIntent().getStringExtra("intro");
//////////////////////////////////////////////////
//        food_name.setText(foodname);
//        price.setText(price1+"元");
//        intro.setText("简介"+intro1);

    }
    void initEvents(){
        showFoodInfo();
        showComments();
        iscollected();
        collect_pic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });
        purchase.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                purchase();
            }
        });

    }

    private void showFoodInfo(){
        GetfoodModel getfoodModel = new GetfoodModel();
        getfoodModel.getFoodById(food_id,this);
    }

    private void showComments(){
        GetfoodModel getfoodModel=new GetfoodModel();
        getfoodModel.getAllCommentsByFood(food_id,this);
    }

    private void iscollected(){
        GetfoodModel getfoodModel=new GetfoodModel();
        getfoodModel.isCollected(user_id,food_id,"1",this);
    }

    private void collect(){
        GetfoodModel getfoodModel=new GetfoodModel();
        getfoodModel.userCollectFood(user_id,food_id,this);
    }

    private void purchase(){
        Intent intent = new Intent(GetFoodActivity.this,PurchaseActivity.class);
        intent.putExtra("food_id",food_id);
        intent.putExtra("price",price1);
        startActivity(intent);
        Toast.makeText(GetFoodActivity.this,"goumai",Toast.LENGTH_SHORT).show();
    }
}
